#include "DlgOpenSample.h"
#include <stdio.h>
#include <QDebug>
#include <QSqlRecord>
#include "dbdata.h"
extern CDBdata *pgdb;

DlgOpenSample::DlgOpenSample(QWidget *parent)
    : QDialog(parent)
{
    ui.setupUi(this);
    model = new QSqlQueryModel(this);
    
    ui.tableView->setModel(model);
    QDateTime db=QDateTime::currentDateTime();
    db.setTime(QTime(0,0,0));
    ui.dateTimeEdit_begin->setDateTime(db);
    QString db_str=db.toString("yyyy-MM-ddThh:mm:ss");
    QDateTime de=QDateTime::currentDateTime();
    de.setTime(QTime(23,59,59));
    QString de_str=de.toString("yyyy-MM-ddThh:mm:ss");
    ui.dateTimeEdit_end->setDateTime(de);

    model->setQuery("select * from sample where mdate between '"+db_str+"' and '"+de_str+"'");
    qDebug()<<db_str<<" "<<de_str;
}
void DlgOpenSample::accept()
{
    //ui.tableView->selectRow();
    //QModelIndexList l=ui.tableView->selectedIndexes () ;
    QItemSelectionModel * l=ui.tableView->selectionModel () ;
    qDebug()<<l;
    //QModelIndexList l1=l->selectedIndexes ();
    QModelIndexList l1=l->selectedRows();
    for(int i=0;i<l1.size();i++)
    {
        //qDebug()<<l1[i];
        int row=l1[i].row();
        QSqlRecord record = model->record(row);
        //int idCol = record.indexOf("id"); // index of the field "name"
        long id=record.value("sampleid").toInt(); // output all names
        qDebug()<<id;
        pgdb->insertTmpid(id);
    }
    QDialog::accept();
}
void DlgOpenSample::reject()
{
    QDialog::reject();
}
    void DlgOpenSample::month_change(){

    }
  void DlgOpenSample:: day_change(){

  }
  void DlgOpenSample:: name_begin_change(){

  }
  void DlgOpenSample:: name_end_change(){

  }
  void DlgOpenSample:: name_include_change(){

  }
  void DlgOpenSample:: name_equal_change(){

  }
  void DlgOpenSample:: use_date(bool){

  }
  void DlgOpenSample:: use_name(bool){

  }
  void DlgOpenSample:: query(){
    QDateTime db=ui.dateTimeEdit_begin->dateTime();
    QString db_str=db.toString("yyyy-MM-ddThh:mm:ss");

    QDateTime de=ui.dateTimeEdit_end->dateTime();
    QString de_str=de.toString("yyyy-MM-ddThh:mm:ss");

    qDebug()<<db_str<<" "<<de_str;
    QString cmd="select * from sample where mdate between '"+db_str+"' and '"+de_str+"'";
    qDebug()<<cmd;
    model->setQuery(cmd);

  }
  void DlgOpenSample:: selectall(){

  }